package com.example.leo.mycharmingstudent.a0916;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.leo.mycharmingstudent.R;

import java.util.List;

/**
 * Created by Leo on 2020/9/16.
 */

public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.MyViewHolder> {
    //可以考虑使用软引用或弱引用
    private Context mContext;
    private List<HeroEntity> mHeroList;
    private RecyclerView mRecyclerView;

    public MyRecyclerViewAdapter(RecyclerView recyclerView, Context context, List<HeroEntity> heroEntityList) {
        this.mContext = context;
        this.mHeroList = heroEntityList;
        this.mRecyclerView = recyclerView;
    }

    /**
     * 创建ViewHolder
     *
     * @param parent
     * @param viewType
     * @return
     */
    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(mContext);
        //inflater.inflate方法的第二个参数，使用parent，可以解决，条目被压缩的问题
        View view = inflater.inflate(
                R.layout.item_cardview_listview_activity,
                parent,
                false);
        return new MyViewHolder(view);
    }

    /**
     * 绑定ViewHolder
     * 要做的事情：设置数据
     * @param holder
     * @param position
     */
    @Override
    public void onBindViewHolder(final MyViewHolder holder, int position) {
        final HeroEntity heroEntity = mHeroList.get(position);
        Glide.with(mContext).load(heroEntity.getImageResource()).into(holder.ivHero);
        holder.tvName.setText(heroEntity.getName());
        holder.tvIntroduce.setText(heroEntity.getIntroduce());
        if (mListener != null) {
            holder.tvName.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mListener.onItemClick(holder.tvName,
                            holder.getAdapterPosition(),
                            heroEntity);
                }
            });
            holder.tvIntroduce.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mListener.onItemClick(holder.tvIntroduce,
                            holder.getAdapterPosition(),
                            heroEntity);
                }
            });
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mListener.onItemClick(holder.itemView,
                            holder.getAdapterPosition(),
                            heroEntity);
                }
            });
        }
    }

    @Override
    public int getItemViewType(int position) {
        return super.getItemViewType(position);
    }

    /**
     * 获取条目数量
     *
     * @return
     */
    @Override
    public int getItemCount() {
        return mHeroList.size();
    }

    /**
     * 静态内部类，不持有外部类的this对象
     * MyViewHolder是静态内部类，MyRecyclerViewAdapter是外部类
     * MyViewHolder不持有MyRecyclerViewAdapter的this对象
     *
     * 再直观一点：
     * mContext是属于MyRecyclerViewAdapter的this对象，
     * 所以不能成功访问
     */
    public static class MyViewHolder extends RecyclerView.ViewHolder {
        private ImageView ivHero;
        private TextView tvName;
        private TextView tvIntroduce;

        public MyViewHolder(View itemView) {
            super(itemView);
            ivHero = itemView.findViewById(R.id.iv_hero_item_cardview);
            tvName = itemView.findViewById(R.id.tv_hero_name_item_cardview);
            tvIntroduce = itemView.findViewById(R.id.tv_hero_introduce_item_cardview);
        }
    }

    private onItemClickListener mListener;

    public void setOnItemClickListener(onItemClickListener onItemClickListener){
        this.mListener = onItemClickListener;
    }

    public interface onItemClickListener{
        void onItemClick(View view,int position,HeroEntity heroEntity);
    }
}
